package com.sinodata.bsm.cicp.collector.jboss;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.jboss.jmx.adaptor.rmi.RMIAdaptor;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.RmiUtil;
import com.sinodata.bsm.common.constants.DataTypeConstants;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: Jboss连通性采集器
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-11-12 14:07:51      liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class JbossConnCollector extends Collector {

    private static Logger logger = Logger.getLogger(JbossConnCollector.class);

    /*
     * (non-Javadoc)
     * @see com.sinodata.bsm.cicp.collector.Collector#collect(java.lang.String)
     */
    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        boolean started = false;
        try {
            RMIAdaptor rmiAdapter = RmiUtil.getRmiAdaptor(ip, Integer.parseInt(this.getParamMap().get("Port")));
            if (rmiAdapter != null)
                started = true;
        } catch (Exception e) {
            logger.warn("jboss (" + ip + ") not started");
            started = false;
        }
        //propValues[0] = ResPropertyValue.createPropValue(started ? "YES" : "NO", propMetas[0]);
        propValues[0] = ResPropertyValue.createPropValue(started ? DataTypeConstants.BOOLEAN_VALUE_TRUE : DataTypeConstants.BOOLEAN_VALUE_FALSE, propMetas[0]);
        propertyValueX.setValues(propValues);
        values.add(propertyValueX);
        return values;
    }

    /*
     * (non-Javadoc)
     * @see com.sinodata.bsm.cicp.collector.Collector#getParamMeta()
     */
    @Override
    public String[] getParamMeta() {
        return new String[] { "Port" };
    }

    /*
     * (non-Javadoc)
     * @see com.sinodata.bsm.cicp.collector.Collector#getPropMeta()
     */
    @Override
    public String[] getPropMeta() {
        //        return new String[] { "ServiceWhetherStarted" };
        return new String[] { "$1" };
    }

}
